package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class RegisterData {
	static Intent intent;
	static FinalDb db;

	/**
	 * 用户注册
	 */
	public static String[] userReg(String strPhone, String strPwd,
			String strName, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/register";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("phone", strPhone));
		params.add(new BasicNameValuePair("username", strName));
		params.add(new BasicNameValuePair("password", strPwd));
		params.add(new BasicNameValuePair("source", "2"));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 获取验证码
	 * 
	 * @param strPhone
	 *            手机号
	 * @param strType
	 *            1是注册时，2是找回登录密码时
	 * @param context
	 */

	public static void userRegPin(String strPhone, String strType,
			Context context) {
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/sendCode?phone=" + strPhone + "&type=" + strType;
		HttpUtils httpUtils = HttpUtils.getInstance();
		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {

			} else {
				Toast.makeText(context, jsonObject.getString("message"),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 注册手机验证码验证
	 * 
	 * @param strPhone
	 * @param strCode
	 * @param context
	 */
	public static String[] phoneCodeVertiy(String strPhone, String strCode,
			Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/phoneCodeVertiy?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();

		params.put("phone", strPhone);
		params.put("code", strCode);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 找回密码
	 * 
	 * @param strPhone
	 * @param strCode
	 * @param context
	 */
	public static String[] userRetPwd(String strPhone, String strCode,
			Context context) {
		String str[] = new String[3];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/vertify?phone=" + strPhone + "&code=" + strCode;
		HttpUtils httpUtils = HttpUtils.getInstance();
		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
			if (str[0].equals("1000")) {
				JSONObject data = new JSONObject(jsonObject.getString("data"));
				str[2] = data.getString("token");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 找回密码，设置新密码
	 * 
	 * @param strPassWord1
	 * @param strPassWord2
	 * @param strToken
	 * @param context
	 */
	public static String[] userRetNewPwd(String strPassWord1,
			String strPassWord2, String strToken, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/passwordReset";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("password", strPassWord1));
		params.add(new BasicNameValuePair("repassword", strPassWord2));
		params.add(new BasicNameValuePair("token", strToken));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 修改登录密码
	 * 
	 * @param strOldPwd
	 * @param strPassWord
	 * @param strRePassWord
	 * @param context
	 */
	public static String[] userChangePwd(String strOldPwd, String strPassWord,
			String strRePassWord, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/updatePassword";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("oldPass", strOldPwd));
		params.add(new BasicNameValuePair("password", strPassWord));
		params.add(new BasicNameValuePair("repassword", strRePassWord));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 修改支付密码
	 * 
	 * @param strOldPwd
	 * @param strPassWord
	 * @param strRePassWord
	 * @param context
	 */
	public static String[] updatePayCode(String strOldPwd, String strPassWord,
			String strRePassWord, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/updatePayCode";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("oldPass", strOldPwd));
		params.add(new BasicNameValuePair("password", strPassWord));
		params.add(new BasicNameValuePair("repassword", strRePassWord));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 找回支付密码
	 * 
	 * @param strPhone
	 * @param strPin
	 * @param strNewPwd
	 * @param strReNewPwd
	 * @param context
	 */
	public static String[] payPwdReset(String strPhone, String strPin,
			String strNewPwd, String strReNewPwd, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/payCodeReset";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		// users.get(0).getToken();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("phone", strPhone));
		params.add(new BasicNameValuePair("code", strPin));
		params.add(new BasicNameValuePair("password", strNewPwd));
		params.add(new BasicNameValuePair("repassword", strReNewPwd));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

}
